package com.ywu.third.exec.standard.exec.impl;

import com.ywu.file.sys.sdk.enter.YwuFileSysExec;
import com.ywu.third.exec.standard.entity.YwuThirdExecEntity;
import com.ywu.third.exec.standard.exec.YwuThirdExecEngine;
import com.ywu.third.exec.standard.exec.param.ParamParseCoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName YwuJavaJarExecEngine
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/10/14 11:20
 * @Version 1.0
 **/
@Component
@Slf4j
public class YwuJavaJarExecEngine implements YwuThirdExecEngine {

    private static final String LOAD_CLASS_NAME = "com.ywu.third.custom.CommonMain";

    private static final String EXEC_MAIN_METHOD_NAME = "coreService";

    @Autowired
    private YwuFileSysExec ywuFileSysExec;

    @Autowired
    private ParamParseCoreService paramParseCoreService;

    private static final String JAVA_JAR_ENGINE_TYPE = "jar";

    /**
    * @Author GroundDemo
    * @Description 执行类型
    * @Date 9:03 2024/10/16
    * @Param []
    * @return java.lang.String
    **/
    @Override
    public String execType() {
        return JAVA_JAR_ENGINE_TYPE;
    }

    /**
    * @Author GroundDemo
    * @Description 执行核心逻辑
    * @Date 9:03 2024/10/16
    * @Param [entity]
    * @return java.lang.Object
    **/
    @Override
    public Object exec(YwuThirdExecEntity entity) {
        // 解析参数
        Map<String, Object> paramMap = paramParseCoreService.parseThirdExecParam(entity);
        // 下载文件
        String filePath = downloadThirdFile(entity, ywuFileSysExec);
        // 加载jar包文件
        File jarFile = new File(filePath);
        URL url = null;
        URLClassLoader classLoader = null;
        Object result = null;
        try {
            // jar包地址
            url = jarFile.toURI().toURL();
            // 获取类加载器
            classLoader = new URLClassLoader(new URL[] { url }, Thread.currentThread()
                    .getContextClassLoader());
            // 加载入口主类
            Class<?> clazz = classLoader.loadClass(LOAD_CLASS_NAME);
            // 获取执行的方法
            Method execMethod = getMethod(clazz, EXEC_MAIN_METHOD_NAME);
            if (Objects.isNull(execMethod)) {
                log.error("exec jar no execMethod error {}", EXEC_MAIN_METHOD_NAME);
                return null;
            }
            // 获取方法中的所有参数名称
            List<Object> params = getExecParams(execMethod, paramMap);
            // 创建对象
            Object tempClass = clazz.newInstance();
            result = execMethod.invoke(tempClass, params.toArray());
        } catch (MalformedURLException | ClassNotFoundException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
            log.error("class exec error {}", e.getMessage());
        } finally {
            // 删除下载的文件
            jarFile.delete();
        }
        return result;
    }
}
